package com.campus.web.controller.study;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.campus.app.service.EveningSubjectsService;
import com.campus.app.service.OnDutysService;
import com.campus.app.service.StudentsService;
import com.campus.commons.entity.*;
import com.campus.commons.entity.Class;
import com.campus.commons.utils.Result;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 晚自习值班表 前端控制器
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@RestController
@RequestMapping("/on-duty")
public class OnDutyController {

    @Autowired
    private OnDutysService dutysService;

@Autowired
private StudentsService studentsService;
    @ApiOperation("获取当天值班的人员")
    @PostMapping("/nowDuty")
    public Result getNowStudent() {
        return dutysService.getNowDuty();
    }

    @GetMapping("/")
    public ModelAndView toCheckEvening(Model model) {
        model.addAttribute("students", studentsService.getStudentByDeId(1).getData());
        return new ModelAndView("learn/on_duty/index");
    }


    //<editor-fold desc="弃用">
    @GetMapping("/edit")
    public ModelAndView toEdit(int id) {
        ModelAndView modelAndView = Edit();
        modelAndView.addObject("edit", true);
        modelAndView.addObject("info",dutysService.getById(id));
        return modelAndView;
    }
    private ModelAndView Edit() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("students", studentsService.getStudentByDeId(1).getData());
        modelAndView.setViewName("learn/on_duty/edit");
        return modelAndView;
    }
    @GetMapping("/add")
    public ModelAndView toAdd() {
        ModelAndView edit = Edit();
        edit.addObject("edit", false);
        return edit;
    }
    //</editor-fold>



    /**
     * 查询所有
     * @param page
     * @param limit
     * @return
     */
    @PostMapping("/all")
    public String getAllDuty(int page,int limit) {
        return JSON.toJSONString(dutysService.getAllDuty(page, limit));
    }
    /**
     * 复合查询
     * @param page
     * @param limit
     * @param start
     * @param end
     * @return
     */
    @PostMapping("/search")
    public String getDutys(int page,int limit,Date start,Date end) {
        return JSON.toJSONString(dutysService.getDutys(page, limit, start, end),SerializerFeature.WriteDateUseDateFormat);
    }

    /**
     * 添加
     * @param onDuty
     * @return
     */
    @PostMapping("/add")
    public String addDuty(OnDuty onDuty) {
        return JSON.toJSONString(dutysService.addDuty(onDuty));
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/del")
    public String delDuty(int id) {
        return JSON.toJSONString(dutysService.delDuty(id));
    }

    /**
     * 更新
     * @param duty
     * @return
     */
    @PostMapping("/update")
    public String upDuty(OnDuty duty,String studentName1,String studentName2) {
        duty.setStudentName1(studentName1);
        duty.setStudentName2(studentName2);
        return JSON.toJSONString(dutysService.upDutyVerify(duty));
    }




}

